'''
Created on Jul 2, 2014

@author: Siyuan Qi
'''

import sys

# Dependencies
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def drawLine(pointA, pointB, ax):
	ax.plot3D([pointA[0], pointB[0]], [pointA[1], pointB[1]], [pointA[2], pointB[2]])

def drawCuboid(cuboid, ax):
	x1 = cuboid[0]
	y1 = cuboid[1]
	x2 = cuboid[2]
	y2 = cuboid[3]
	x3 = cuboid[4]
	y3 = cuboid[5]
	x4 = cuboid[6]
	y4 = cuboid[7]

	z1 = cuboid[8]
	z2 = cuboid[9]

	A = [x1, y1, z1]
	B = [x2, y2, z1]
	C = [x3, y3, z1]
	D = [x4, y4, z1]
	E = [x1, y1, z2]
	F = [x2, y2, z2]
	G = [x3, y3, z2]
	H = [x4, y4, z2]

	drawLine(A, B, ax)
	drawLine(B, C, ax)
	drawLine(C, D, ax)
	drawLine(D, A, ax)
	drawLine(E, F, ax)
	drawLine(F, G, ax)
	drawLine(G, H, ax)
	drawLine(H, E, ax)
	drawLine(A, E, ax)
	drawLine(B, F, ax)
	drawLine(C, G, ax)
	drawLine(D, H, ax)

	return

def savePlotToFile(cuboidList, saveFilename):
	fig = plt.figure()
	ax = fig.gca(projection='3d')
	ax.set_aspect("equal")

	for cuboid in cuboidList:
		drawCuboid(cuboid, ax)
		
	plt.savefig(saveFilename)
	plt.clf()

def plotFromFile(filename):
	fig = plt.figure()
	ax = fig.gca(projection='3d')
	ax.set_aspect("equal")

	fCuboids = open(filename, 'r')
	for cuboid in fCuboids:
		drawCuboid([float(p) for p in cuboid.split()], ax)

	plt.show()

def plotFromList(cuboidList):
	fig = plt.figure()
	ax = fig.gca(projection='3d')
	ax.set_aspect("equal")

	for cuboid in cuboidList:
		drawCuboid(cuboid, ax)

	plt.show()
